﻿using System;
using System.Web.Mvc;
using Unijovem.Domain;

namespace Unijovem.UI.Controllers
{
    public class NewsLetterController : Controller
    {
        [HttpPost]
        public ActionResult CadastrarEmail(string email)
        {
            if (new NewsLetter {Email = email}.EmailCadastrado)
                return Json(new { success = true, mensagem = "Email já está cadastrado." });    

            new NewsLetter { DataCadastro = DateTime.Now, Email = email }.Save();
            return Json(new { success = true, mensagem = "Email cadastrado com sucesso!" });    
        }

        public ActionResult Excluir(string email)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Excluir(FormCollection collection)
        {
            var motivo = collection.GetValue("Motivo").AttemptedValue;

            var newsLetter = new NewsLetter().SelecionarPorEmail(Request.QueryString.Get("email"));

            if (newsLetter == null) return View();

            newsLetter.DataExclusao = DateTime.Now;
            newsLetter.MotivoExclusao = int.Parse(motivo);
            newsLetter.Save(newsLetter);

            return View();
        }
    }
}
